<?php
    $conn = new mysqli('localhost', 'root', 'root', 'sy2108');

    if ($conn -> connect_error) {
        die('连接错误');
    }

    $u = $_POST['username'];

    $sql = "SELECT * FROM `user` WHERE `username`='$u'";

    $res = $conn -> query($sql);

    $obj = mysqli_fetch_assoc($res);

    if ($obj) {
        // 后台只能给前端返回一个对象（字符串类型）
        echo json_encode(array(
            "message" => "手机号已被注册",
            "state" => false
        ));
        // setcookie('token', "2903j9hf8923rhu2i3huygdsuyfgusyf239ye92ge8wgf7298");
    } else {
        // 肯定是没被注册
        $sql2 = "INSERT INTO `user` (`username`, `password`, `age`, `sex`) VALUES ('$u', '123456', 18, '男')";
        $res2 = $conn -> query($sql2);

        if (res2) {
            echo json_encode(array(
                "message" => "注册成功",
                "state" => true,
                "code" => 123456,
                 "token" => "2903j9hf8923rhu2i3huygdsuyfgusyf239ye92ge8wgf7298"
            ));
        } else {
            echo json_encode(array(
                "message" => "注册失败",
                "state" => false,
                "token" => null
            ));
        }
    }
?>